Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
java-caller
Advanced tools
Library to easily call java from node sources. Automatically installs java if not present
Lightweight cross-platform javascript module to easily call java commands from Node.js sources.
There are two ways to use java-caller:
npm install java-caller --save
const JavaCaller = require('java-caller');
const java = new JavaCaller(JAVA_CALLER_OPTIONS);
const {status, stdout, stderr} = java.run(JAVA_ARGUMENTS,JAVA_CALLER_RUN_OPTIONS);
Parameter | Description | Default value | Example |
---|---|---|---|
jar | Path to executable jar file | "myfolder/myjar.jar" | |
classPath | If jar parameter is not set, classpath to use Use : as separator (it will be converted if runned on Windows), or use a string array. | . (current folder) | "java/myJar.jar:java/myOtherJar.jar" |
useAbsoluteClassPaths | Set to true if classpaths should not be based on the rootPath | false | true |
mainClass | If classPath set, main class to call | "com.example.MyClass" | |
rootPath | If classPath elements are not relative to the current folder, you can define a root path. You may use __dirname if you classes / jars are in your module folder | . (current folder) | "/home/my/folder/containing/jars" |
minimumJavaVersion | Minimum java version to be used to call java command. If the java version found on machine is lower, java-caller will try to install and use the appropriate one | 8 | 11 |
maximumJavaVersion | Maximum java version to be used to call java command. If the java version found on machine is upper, java-caller will try to install and use the appropriate one Can be equal to minimumJavaVersion | 10 | |
javaType | jre or jdk (if not defined and installation is required, jre will be installed) | "jre" | |
additionalJavaArgs | Additional parameters for JVM that will be added in every JavaCaller instance runs | ["-Xms256m","-Xmx2048m"] | |
javaExecutable | You can force to use a defined java executable, instead of letting java-caller find/install one. Can also be defined with env var JAVA_CALLER_JAVA_EXECUTABLE | "/home/some-java-version/bin/java.exe" |
The list of arguments can contain both arguments types together:
"-Xms256m"
, "-Xmx2048m"
public static void main method
). ex: "--someflag"
, "--someflagwithvalue myVal"
, "-c"
Example: ["-Xms256m", "--someflagwithvalue myVal", "-c"]
Parameter | Description | Default | Example |
---|---|---|---|
detached | If set to true, node will node wait for the java command to be completed. In that case, childJavaProcess property will be returned, but stdout and stderr may be empty, except if an error is triggered at command execution | false | true |
stdoutEncoding | Adds control on spawn process stdout | utf8 | ucs2 |
waitForErrorMs | If detached is true, number of milliseconds to wait to detect an error before exiting JavaCaller run | 500 | 2000 |
cwd | You can override cwd of spawn called by JavaCaller runner | process.cwd() | some/other/cwd/folder |
javaArgs | List of arguments for JVM only, not the JAR or the class | [] | ['--add-opens=java.base/java.lang=ALL-UNNAMED'] |
windowsVerbatimArguments | No quoting or escaping of arguments is done on Windows. Ignored on Unix. This is set to true automatically when shell is specified and is CMD. | true | false |
windowless | If windowless is true, JavaCaller calls javaw instead of java to not create any windows, useful when using detached on Windows. Ignored on Unix. | false | true |
Call a class located in classpath
const java = new JavaCaller({
classPath: 'test/java/dist',
mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run();
Call a class with multiple folders in the classPath
const java = new JavaCaller({
classPath: ['C:\\pathA\\test\\java\\dist', 'C:\\pathB\\test\\java\\dist'],
mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run();
Call a class located in classpath with java and custom arguments
const java = new JavaCaller({
classPath: 'test/java/dist',
mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run(['-Xms256m', '-Xmx2048m', '--customarg nico']);
Call a class in jar located in classpath
const java = new JavaCaller({
classPath: 'test/java/jar/JavaCallerTester.jar',
mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run();
Call a runnable jar
const java = new JavaCaller({
jar: 'test/java/jar/JavaCallerTesterRunnable.jar',
});
const { status, stdout, stderr } = await java.run();
Call a detached java process
const java = new JavaCaller({
classPath: 'test/java/dist',
mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr, childJavaProcess } = await java.run(['--sleep'], { detached: true });
// Kill later the java process if necessary
childJavaProcess.kill('SIGINT');
Call a windowless java process
const java = new JavaCaller({
classPath: 'test/java/dist',
mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run(['--sleep'], { windowless: true });
You can see more examples in test methods
Set environment variable DEBUG=java-caller
before calling your code using java-caller module, and you will see the java commands executed.
Example debug log:
java-caller Found Java version 1.80131 +1s
java-caller Java command: java -Xms256m -Xmx2048m -cp C:\Work\gitPerso\node-java-caller\test\java\dist com.nvuillam.javacaller.JavaCallerTester -customarg nico +1ms
Contributions are very welcome !
Please follow Contribution instructions
See complete CHANGELOG
[3.3.1] 2024-04-28
FAQs
Library to easily call java from node sources. Automatically installs java if not present
The npm package java-caller receives a total of 5,708 weekly downloads. As such, java-caller popularity was classified as popular.
We found that java-caller demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.